package com.ydgk.controller;

import com.ydgk.bean.*;
import com.ydgk.service.OrderService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    private static final Integer pageSize = 3;

    @ModelAttribute
    public void getPre_order(@RequestParam(value = "poid",required = false,defaultValue = "0")Integer poid, Map map){
        if(poid != 0){
            Pre_order pre_order = orderService.findPreById(poid);
            map.put("pre_order",pre_order);
        }
    }

    @ModelAttribute
    public void getOrder(@RequestParam(value = "oid",required = false,defaultValue = "0")Integer oid, Map map){
        if(oid != 0){
            Order order = orderService.findOrderById(oid);
            map.put("order",order);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/insertPre_order")
    public AjaxResult insertPre_order(Pre_order pre_order){
        AjaxResult ajaxResult = new AjaxResult();
        orderService.insertPre_order(pre_order);
        ajaxResult.setData(pre_order);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/getPreCount")
    public AjaxResult getPreCount(Integer uid){
        AjaxResult ajaxResult = new AjaxResult();
        int count = orderService.getPreCount(uid);
        ajaxResult.setData(count);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/getPre")
    public AjaxResult getPre(Integer uid){
        AjaxResult ajaxResult = new AjaxResult();
        List<Pre_order> pre_orderList = orderService.getPre(uid);
        ajaxResult.setData(pre_orderList);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @RequestMapping(value = "/deletePre")
    public String deletePre(Integer poid){
        orderService.deletePre_order(poid);
        return "redirect:/toTrolley";
    }

    @ResponseBody
    @RequestMapping(value = "/deletePres")
    public AjaxResult deletePres(@RequestParam(value = "poids[]")  Integer[]  poids) {
        AjaxResult ajaxResult = new AjaxResult();
        for (int i = 0; i < poids.length; i++) {
            orderService.deletePre_order(poids[i]);
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/updatePre")
    public AjaxResult updatePre(Pre_order pre_order){
        orderService.updatePre_order(pre_order);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/insertOrder")
    public AjaxResult insertOrder(@RequestParam(value = "poids[]")  Integer[]  poids, Order order, @RequestParam("eWay") String eWay){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            orderService.insertOrder(poids,order,eWay);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/findAllOrder")
    public AjaxResult findAllOrder(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                   @RequestParam(value = "otype", required = false, defaultValue = "") Integer otype,
                                   @RequestParam(value = "likeName" , required = false, defaultValue = "") String likeName,
                                   @RequestParam(value = "methodName" , required = false, defaultValue = "") String methodName,HttpSession session){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            User user = (User)session.getAttribute("user");
            Map<String, Object> factorMap = new HashMap<String, Object>();
            factorMap.put("otype", otype);
            factorMap.put("likeName",likeName);
            factorMap.put("uid",user.getUid());
            int count = orderService.findAllOrderCount(factorMap);
            Integer pageTotal = 0;
            if(count <= pageSize){
                pageTotal = 1;
            }else{
                if (count % pageSize == 0) {
                    pageTotal = count / pageSize;
                } else {
                    pageTotal = count / pageSize + 1;
                }
            }
            if(pageNo > pageTotal){
                pageNo = pageTotal;
            }
            if(pageNo < 1){
                pageNo = 1;
            }

            Integer pageStart = (pageNo - 1) * pageSize;
            factorMap.put("start", pageStart);
            factorMap.put("size", pageSize);
            List<Order> orderList = orderService.findAllOrder(factorMap);
            List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
            for (Order o : orderList) {
            OrderInfo orderInfo = orderService.getOrderInfo(o);
            orderInfoList.add(orderInfo);
            }
            String pageContent = Tool.getPageContentForPortal(pageNo,pageTotal,methodName);
            Page<OrderInfo> page = new Page(orderInfoList, pageNo, pageTotal,count,pageContent);
            ajaxResult.setData(page);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @RequestMapping(value = "/finishOrder")
    public String finishOrder(Order order){
        order.setOtype(2);
        orderService.updateOrder(order);
        return "redirect:/toMyOrder";
    }

    @ResponseBody
    @RequestMapping(value = "/findOrderCountByOtype")
    public AjaxResult findOrderCountByOtype(Integer otype, HttpSession session){
        User user = (User)session.getAttribute("user");
        int count = orderService.findOrderCountByOtype(otype,user.getUid());
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setData(count);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
}
